package com.cskaoyan.user.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserVerifyInfoDto;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Date 2022-12-03 12:58
 * @Created by yepq2025@outlook.com
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserConverterMapper userConverterMapper;

    /**
     * TODO: 用户注册激活
     * author: yepq2025@outlook.com
     * time: 2022-12-03 12:59:31
     */
    @Override
    public UserVerifyResponse verifyUser(UserVerifyRequest verifyRequest) {
        UserVerifyResponse response = new UserVerifyResponse ( );
        try {
            // 判空
            verifyRequest.requestCheck ( );
            // 判断uuid和username是否存在
            Example example = new Example ( UserVerify.class );
            example.createCriteria ( )
                    .andEqualTo ( "uuid", verifyRequest.getUuid ( ) )
                    .andEqualTo ( "username", verifyRequest.getUserName ( ) );
            // 得到tb_user_verify里面的数据
            List<UserVerify> userVerifyList = userVerifyMapper.selectByExample ( example );
            // 判空
            if (CollectionUtils.isEmpty ( userVerifyList )) {
                throw new ValidateException ( UserRetCode.DATA_NOT_EXIST.getCode ( ), UserRetCode.DATA_NOT_EXIST.getMessage ( ) );
            }
            // 判断用户是否已经激活
            if ("Y".equals ( userVerifyList.get ( 0 ).getIsVerify ( ) )) {
                throw new ValidateException ( UserRetCode.DATA_NOT_EXIST.getCode ( ), UserRetCode.DATA_NOT_EXIST.getMessage ( ) );
            }
            // 走到这里，用户未激活  激活步骤
            // 修改两张表里的字段 N--Y
            UserVerify userVerify = new UserVerify ( );
            userVerify.setIsVerify ( "Y" );
            userVerifyMapper.updateByExampleSelective ( userVerify, example );

            Example exampleMember = new Example ( Member.class );
            Member member = new Member ( );
            exampleMember.createCriteria ( )
                    .andEqualTo ( "username", verifyRequest.getUserName ( ) );
            member.setIsVerified ( "Y" );
            memberMapper.updateByExampleSelective ( member, exampleMember );
            // 返回成功标志
            response.setCode ( UserRetCode.SUCCESS.getCode ( ) );
            response.setMsg ( UserRetCode.SUCCESS.getMessage ( ) );
            return response;
        } catch (ValidateException e) {
            ExceptionProcessorUtils.wrapperHandlerException ( response, e );
        }
        return response;
    }

    @Override
    public UserVerifyInfoDto getUserVerifyInfo(Long userId) {
        Member member = memberMapper.selectByPrimaryKey(userId);
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", member.getUsername());
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        UserVerify userVerify = userVerifies.get(0);
        UserVerifyInfoDto userVerifyInfoDto = userConverterMapper.userInfoConverter(userVerify);
        return userVerifyInfoDto;
    }
}
